<?php
include_once 'protoweb_controller.php';

/**--------------------------------------------------------------------------
 * Please, don't touch this file.
 *
 * If you need to custom this Class, you can edit froms.php
 * file overwrite and define new methods
 *---------------------------------------------------------------------------
 */

/**
 * @package Protoweb
 * @subpackage controller
 */
class Froms_protoweb extends Protoweb_controller {

  public function __construct() {
    parent::__construct();
  } // end of method __construct()



  public function edit($id=0) {
    $data["from"] = new From($id);

    if ($this->input->post("posting") == 1)
      $data = $this->edit_save($data["from"]->id);

    // To one relationships
        
    $clients = new Client();
    $data["clients"] = $clients->all_to_asoc_array("id","name");
    $data["clients_selected"] = $data["from"]->client->id;
    
    // To many relationships
    
    $this->smarty_parser->view('from_edit.html', $data);
  } // end of method edit($id=0)


  private function edit_save($id) {
    $data["error"] = "";
    $data["from"] = new From($id);

          // Assign value to field. If value is empty, we will assign NULL
      $data["from"]->email = $this->input->post("email");
      if ($data["from"]->email == "") $data["from"]->email = NULL;
      
      // files for upload
      /*
      if ($data["from"]->email->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(email)) $data["from"]->email = $fileupload.clean_file_name(email);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
          // Assign value to field. If value is empty, we will assign NULL
      $data["from"]->name = $this->input->post("name");
      if ($data["from"]->name == "") $data["from"]->name = NULL;
      
      // files for upload
      /*
      if ($data["from"]->name->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(name)) $data["from"]->name = $fileupload.clean_file_name(name);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
          // Assign value to field. If value is empty, we will assign NULL
      $data["from"]->lastused = $this->input->post("lastused");
      if ($data["from"]->lastused == "") $data["from"]->lastused = NULL;
      
      // files for upload
      /*
      if ($data["from"]->lastused->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(lastused)) $data["from"]->lastused = $fileupload.clean_file_name(lastused);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
          // Assign value to field. If value is empty, we will assign NULL
      $data["from"]->verified = $this->input->post("verified");
      if ($data["from"]->verified == "") $data["from"]->verified = NULL;
      
      // files for upload
      /*
      if ($data["from"]->verified->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(verified)) $data["from"]->verified = $fileupload.clean_file_name(verified);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
    
    //saving attributes
    if(! $data["from"]->save())
      $data["error"] .= $data["from"]->error->string;

    //saving to one relationships
    
    $client = new Client($this->input->post("client"));
    if (! $data["from"]->save(array($client)))
      $data["error"] .= $data["from"]->error->string;
    
    //to many relationships
    
    redirect(base_url()."froms/edit/".$data["from"]->id);
  } // end of method edit_save($id)


  public function delete($id) {
    $data["error"] = "";
    $data["from"] = new From($id);

    //delete
    if(! $data["from"]->delete())
      $data["error"] .= $data["from"]->error->string;

    redirect(base_url()."froms/edit/");
  } // end of method delete($id)

    
} // class Froms_protoweb

/* End of file froms_protoweb.php */
/* Location: ./system/application/controllers/froms_protoweb.php */
